package main

import "github.com/pion/webrtc/v2"

var (
	pubTrans *Trans
	subTrans *Trans
)

type Trans struct {
	mediaEngine  webrtc.MediaEngine
	api          *webrtc.API
	pc           *webrtc.PeerConnection
	audioTrack   *webrtc.Track
	videoTrack   *webrtc.Track
	isPub        bool
	iceConnected chan bool
}

func NewTrans(isPub bool) *Trans {
	w := &Trans{
		isPub:        isPub,
		iceConnected: make(chan bool),
	}
	err := w.init()
	w.pc, err = w.api.NewPeerConnection(config)
	if err != nil {
		panic(err)
	}

	if isPub {

	} else {

	}

	return w
}

func (w *Trans) init() error {
	w.mediaEngine = webrtc.MediaEngine{}
	config = webrtc.Configuration{
		ICEServers: []webrtc.ICEServer{
			{
				URLs: []string{"stun:stun1.l.google.com:19302"},
				//URLs: []string{"stun:115.231.90.179:3478"},
			},
		},
	}
	w.mediaEngine = webrtc.MediaEngine{}
	w.mediaEngine.RegisterCodec(webrtc.NewRTPOpusCodec(webrtc.DefaultPayloadTypeOpus, 48000))
	w.mediaEngine.RegisterCodec(webrtc.NewRTPH264Codec(webrtc.DefaultPayloadTypeH264, 90000))
	w.api = webrtc.NewAPI(webrtc.WithMediaEngine(w.mediaEngine))
	return nil
}
